<?php

namespace app\admin\controller;


use app\admin\model\OptionModel;
use think\Db;
 
class Option extends Base
{

    /**
     * [index 轮播列表]
     * @return [type] [description]
     * @author felix
     */
    public function index(){

        $key = input('key');  //商品名称
        $question_id = input('question_id');  //商品分类
        $cat_id = input('cat_id');  //商品分类
        $map = [];
        //$map['is_show'] = 1;
        if($key&&$key!==""){
            $map['name'] = ['like',"%" . $key . "%"];
        }

        if($question_id&&$question_id!==""){
            $map['question_id'] = $question_id;
        }

        if($cat_id&&$cat_id!==""){
            $map['cat_id'] = $cat_id;
        }

        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('option')->where($map)->count();
        $allpage = intval(ceil($count / $limits));
        $goods = new OptionModel();
        $lists = $goods->getDataByWhere($map, $Nowpage, $limits);
        if($lists){
            foreach ($lists as $k => $v){
                $lists[$k]['q_name'] = model('QuestionModel')->where('id',$v['question_id'])->value('name')??'';
                $lists[$k]['c_name'] = model('OptionCatModel')->where('id',$v['cat_id'])->value('name')??'';
            }
        }
        //dump($lists);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('val', $key);
        $this->assign('question_id', $question_id);
        $this->assign('cat_id', $cat_id);
        $option_cat = model('OptionCatModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option_cat',$option_cat);

        $question = model('QuestionModel')->order('sort asc')->field('id,name')->select();
        $this->assign('question',$question);


        $no_ajax = input('no_ajax') ? true : false;
        $this->assign('page', $Nowpage);
        if(input('get.page') && !$no_ajax)
        {
            return json($lists);
        }
        return $this->fetch();
    }




    /**
     * @return [type] [description]
     * @author felix
     */
    public function optionAdd()
    {
        if(request()->isAjax()){

            $param = input('post.');
            //dump($param);die;
            $goods = new OptionModel();
            $flag = $goods->insertData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        //获取商品分类
        $option_cat = model('OptionCatModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option_cat',$option_cat);

        $question = model('QuestionModel')->order('sort asc')->field('id,name')->select();
        $this->assign('question',$question);

        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function optionEdit()
    {
        $goods = new OptionModel();
        $page = input('page');
        if(request()->isAjax()){

            $param = input('post.');
            $flag = $goods->updateData($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }

        $option_cat = model('OptionCatModel')->order('sort asc')->field('id,name')->select();
        $this->assign('option_cat',$option_cat);

        $question = model('QuestionModel')->order('sort asc')->field('id,name')->select();
        $this->assign('question',$question);

        $id = input('param.id');
        $this->assign('option',$goods->getOneData($id));
        $this->assign('page', $page);
        return $this->fetch();
    }


    /**
     * @return [type] [description]
     * @author felix
     */
    public function optionDel()
    {
        $id = input('param.id');
        $goods = new OptionModel();
        $flag = $goods->delData($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }





    /**
     * @return [type] [description]
     * @author felix
     */
    public function option_show_state()
    {
        $id = input('param.id');
        $status = Db::name('option')->where('id',$id)->value('is_show');//判断当前状态情况
        if($status==1)
        {
            $flag = Db::name('option')->where('id',$id)->update(['is_show'=>-1]);
            return json(['code' => 1, 'data' => $flag['data'], 'msg' => '已下架']);
        }
        else
        {
            $flag = Db::name('option')->where('id',$id)->update(['is_show'=>1]);
            return json(['code' => 0, 'data' => $flag['data'], 'msg' => '已上架']);
        }
    }




}